package com.wwwarehouse.taskcenter.fragment.job_point.step7;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.GridView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.adapter.job_point.TemplateAdapter;
import com.wwwarehouse.taskcenter.bean.job_point.JobPointRuleTemplateResponseBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_BUSINESS_ID;

/**
 * Created by Jackie on 2017/11/13.
 * 设置作业点规则 选择添加方式 存放规则  详情
 */

public class ChooseAddWayRuleTemplateDetailsFragment extends BaseFragment {
    private View mRootView;
    private StateLayout mStateLayout;
    private GridView mGridView;
    private TemplateAdapter mAdapter;

    private String mBusinessId;

    public static final String KEY_TEMPLATE_ID = "key_template_id";

    private static final int REQUEST_GET_TEMPLATE_INFO = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_choose_add_way_rule_template_details, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        if (enter) {
            getTemplateInfo();
        }

        return null;
    }

    private void initView() {
        mStateLayout = findView(mRootView, R.id.sl_layout);
        mGridView = findView(mRootView, R.id.gv_details);
    }

    private void initData() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            mBusinessId = bundle.getString(KEY_BUSINESS_ID);
        }
    }

    private void initEvent() {

    }


    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }

    NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_GET_TEMPLATE_INFO:
                    if ("0".equals(commonClass.getCode())) {
                        JobPointRuleTemplateResponseBean jobPointRuleTemplateResponseBean = JSON.parseObject(commonClass.getData().toString(), JobPointRuleTemplateResponseBean.class);
                        final List<JobPointRuleTemplateResponseBean.JobPointTemplateDetailsBean> jobPointRuleTemplateDetailsList = jobPointRuleTemplateResponseBean.getList();

                        mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                            @Override
                            public void onGlobalLayout() {
                                mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                                int itemHeight = (mGridView.getMeasuredHeight() - mGridView.getPaddingTop() - mGridView.getPaddingBottom() - mGridView.getVerticalSpacing() * 3) / 4;

                                mAdapter = new TemplateAdapter(mActivity, R.layout.item_add_way_rule_template_details, jobPointRuleTemplateDetailsList, itemHeight);

                                mGridView.setAdapter(mAdapter);

                                mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                        JobPointRuleTemplateResponseBean.JobPointTemplateDetailsBean jobPointTemplateDetailsBean = jobPointRuleTemplateDetailsList.get(position);

                                        Bundle bundle = new Bundle();
                                        bundle.putString(KEY_BUSINESS_ID, mBusinessId);
                                        bundle.putString(KEY_TEMPLATE_ID, jobPointTemplateDetailsBean.getTempId());

                                        ChooseAddWayAddRuleFragment chooseAddWayAddRuleFragment = new ChooseAddWayAddRuleFragment();
                                        chooseAddWayAddRuleFragment.setArguments(bundle);

                                        pushFragment(chooseAddWayAddRuleFragment);
                                    }
                                });
                            }
                        });
                    } else {
                        mStateLayout.showSystemView(commonClass.getMsg(), false);
                        mStateLayout.setSystemListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mStateLayout.showProgressView(false);

                                getTemplateInfo();
                            }
                        });
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onFailed(String s, int i) {
            mStateLayout.showNetworkView(false);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mStateLayout.showProgressView(false);

                    getTemplateInfo();
                }
            });
        }

        @Override
        public void onFinish(int i) {
            mStateLayout.showContentView();
        }
    };

    /**
     * 查询模板
     */
    private void getTemplateInfo() {
        mStateLayout.showProgressView(false);

        Bundle bundle = getArguments();

        if (bundle != null) {
            int page = bundle.getInt("page");

            Map map  = new HashMap();

            map.put("page", page);
            map.put("size", 10);

            NoHttpUtils.httpPost(TaskCenterConstant.URL_GET_TEMPLATE_INFO, map, mOnResponseListener, REQUEST_GET_TEMPLATE_INFO);
        }
    }
}
